package org.eclipse.epsilon.emc.spreadsheets.csv.dt;

import org.eclipse.epsilon.common.dt.launching.dialogs.AbstractCachedModelConfigurationDialog;
import org.eclipse.epsilon.emc.spreadsheets.csv.CsvConstants;
import org.eclipse.epsilon.emc.spreadsheets.csv.CsvModel;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class CsvModelConfigurationDialogue extends AbstractCachedModelConfigurationDialog
{
	protected Label spreadsheetFileTextLabel;
	protected Text spreadsheetFileText;
	protected Button browseSpreadsheetFile;
	protected Label configFileTextLabel;
	protected Text configurationFileText;
	protected Button browseConfigFile;

	private Text fieldSeparatorText;
	private Button hasHeaderCheckbox;

	@Override
	protected String getModelName()
	{
		return "CSV Model";
	}

	@Override
	protected String getModelType()
	{
		return "CSVModel";
	}

	@Override
	protected void createGroups(Composite control)
	{
		super.createGroups(control);
		createFileGroup(control);
		createLoadStoreOptionsGroup(control);
		createCsvGroup(control);
		createConfigurationFileGroup(control);
	}

	private void createFileGroup(Composite parent)
	{
		final Composite groupContent = createGroupContainer(parent, "CSV File", 3);

		spreadsheetFileTextLabel = new Label(groupContent, SWT.NONE);
		spreadsheetFileTextLabel.setText("File: ");

		spreadsheetFileText = new Text(groupContent, SWT.BORDER);
		spreadsheetFileText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		browseSpreadsheetFile = new Button(groupContent, SWT.NONE);
		browseSpreadsheetFile.setText("Browse Workspace...");
		browseSpreadsheetFile.addListener(SWT.Selection, new BrowseWorkspaceForModelsListener(spreadsheetFileText,
				"CSV File in the workspace", "Select CSV File"));

		groupContent.layout();
		groupContent.pack();
	}

	protected void createConfigurationFileGroup(final Composite parent)
	{
		final Composite groupContent = createGroupContainer(parent, "ORM Meta-data", 3);

		configFileTextLabel = new Label(groupContent, SWT.NONE);
		configFileTextLabel.setText("File: ");

		configurationFileText = new Text(groupContent, SWT.BORDER);
		configurationFileText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		browseConfigFile = new Button(groupContent, SWT.NONE);
		browseConfigFile.setText("Browse Workspace...");
		browseConfigFile.addListener(SWT.Selection, new BrowseWorkspaceForModelsListener(configurationFileText,
				"XML meta-data file in the workspace", "Select an XML Meta-data File"));

		groupContent.layout();
		groupContent.pack();
	}

	protected void createCsvGroup(Composite parent)
	{
		final Composite groupContent = createGroupContainer(parent, "CSV File Configuration", 4);

		final Label modelFieldSeparatorLabel = new Label(groupContent, SWT.NONE);
		modelFieldSeparatorLabel.setText("Field Separator: ");
		fieldSeparatorText = new Text(groupContent, SWT.BORDER);
		fieldSeparatorText.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));

		hasHeaderCheckbox = new Button(groupContent, SWT.CHECK);
		hasHeaderCheckbox.setText("Has Header");
	}

	@Override
	protected void loadProperties()
	{
		super.loadProperties();
		if (properties == null)
		{
			fieldSeparatorText.setText(CsvConstants.DEFAULT_SEPARATOR);
			hasHeaderCheckbox.setSelection(true);
		}
		else
		{
			spreadsheetFileText.setText(properties.getProperty(CsvModel.PROPERTY_CSV_FILE));
			fieldSeparatorText.setText(properties.getProperty(CsvModel.PROPERTY_SEPARATOR));
			hasHeaderCheckbox.setSelection(properties.getBooleanProperty(CsvModel.PROPERTY_HAS_HEADER, false));
		}
	}

	@Override
	protected void storeProperties()
	{
		super.storeProperties();
		properties.setProperty(CsvModel.PROPERTY_CSV_FILE, spreadsheetFileText.getText());
		properties.setProperty(CsvModel.PROPERTY_SEPARATOR, fieldSeparatorText.getText());
		properties.setProperty(CsvModel.PROPERTY_HAS_HEADER, String.valueOf(hasHeaderCheckbox.getSelection()));
	}
}
